package com.chunsoft.intent_service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;

import java.util.Random;

public class MyService extends Service {
    private  String TAG = "MyService";
    private Binder localBinder = new LocalBinder();
    private final Random random = new Random();
    public MyService() {
    }

    public class LocalBinder extends Binder{
        public MyService getService(){
            return MyService.this;
        }

        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
                throws RemoteException {
            Log.e(TAG,"---从Activity中获取的值－－－>"+data.readInt());
            Log.e("TAG","---从Activity中获取的值－－－>"+data.readString());
            reply.writeInt(getRandom());
            reply.writeString("rose+jact");
            return super.onTransact(code, data, reply, flags);
        }
    }

    /**
     * 在Service自定义的方法，提供给Client使用，通常是Activity
     */
    public int getRandom()
    {
        return random.nextInt(100);
    }
    @Override
    public void onCreate() {
        Log.i(TAG,"--------->onCreate");
        super.onCreate();

    }



    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG,"--------->onBind");
        return localBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG,"--------->onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.i(TAG,"--------->onRebind");
        super.onRebind(intent);
    }

    @Override
    public void onDestroy() {
        Log.i(TAG,"--------->onDestroy");
        super.onDestroy();
    }
}
